/**
 * 
 */
package org.swing.utility.jai.file;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;

import java.io.FileNotFoundException;
import java.io.IOException;
/**
 * @author Quynh Nhu
 *
 */
public class FileAccess 
{

    /**
     * Move the srcFile to the destination folder.
     * @param srcFile FileAccess to move.
     * @param destFolder Path to folder where source file will be moved.
     * @return True if successfully moved, else false is returned.
     * @note If false is returned, check if source and destination exist.
     */
    public static boolean Move(String srcFile, String destFolder)
    {
        // FileAccess (or directory) to be moved
        java.io.File file = new java.io.File(srcFile);

        if(!file.exists())
            return false;
        // Destination directory
        java.io.File dir = new java.io.File(destFolder);

        if(!dir.exists())
            return false;

        // Move file to new directory
        boolean success = file.renameTo(new java.io.File(dir, file.getName()));

        return success;
    }

    public static boolean Move(String srcFile, String destFolder, boolean overwrite)
    {
        // FileAccess (or directory) to be moved
        java.io.File file = new java.io.File(srcFile);

        if(overwrite)
        {
            String sf = file.getName();
            String newPath = Path.FixPath(destFolder);

            java.io.File dest = new java.io.File(newPath + sf);

            if(dest.exists())
                dest.delete();
        }

        if(!file.exists())
            return false;
        // Destination directory
        java.io.File dir = new java.io.File(destFolder);

        if(!dir.exists())
            return false;

        // Move file to new directory
        boolean success = file.renameTo(new java.io.File(dir, file.getName()));

        return success;
    }

    /**
     * CopyFile from source to destination.
     * 
     * @param srFile Source file name.
     * @param dtFile Destination file name.
     * 
     * @throws java.lang.Exception
     */
    public static void CopyFile(String srFile, String dtFile) throws Exception
    {
        try
        {
            File f1 = new File(srFile);
            File f2 = new File(dtFile);
            InputStream in = new FileInputStream(f1);

            OutputStream out = new FileOutputStream(f2);

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0)
            {
                out.write(buf, 0, len);
            }

            in.close();
            out.close();

        }
        catch(FileNotFoundException ex)
        {
            throw new Exception("File not found: " + ex.toString());
        }
        catch(IOException e)
        {
            throw new Exception(e.toString());
        }
    }
}
